package com.keensense.vcm.internal;

import static com.keensense.vcm.common.parser.ResponseParsers.queryArchivesLibrariesResponseParser;
import static com.keensense.vcm.internal.VCMResourcePathConstants.BATCH_QUERY_ARCHIVES_LIBRARIES;
import static com.keensense.vcm.internal.VCMResourcePathConstants.PROFILE_LIBRARIES;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import com.keensense.vcm.HttpMethod;
import com.keensense.vcm.common.auth.CredentialsProvider;
import com.keensense.vcm.common.comm.RequestMessage;
import com.keensense.vcm.common.comm.ServiceClient;
import com.keensense.vcm.common.parser.ResponseParsers;
import com.keensense.vcm.model.*;

/**
 * @author lanjian
 * @date 2020/5/18 11:13 上午
 */
public class ProfileLibraryOperation extends VCMOperation {

    public ProfileLibraryOperation(ServiceClient client, CredentialsProvider credsProvider) {
        super(client, credsProvider);
    }

    /**
     * 获取某个档案库信息
     *
     * @param pId
     * @return
     */
    public QueryProfileLibrariesResponse queryById(String pId) {
        String resourcePath = PROFILE_LIBRARIES + "/" + pId;
        RequestMessage request = new VCMRequestMessageBuilder(this.client).setEndpoint(this.endpoint).setMethod(HttpMethod.GET).build();
        request.setResourcePath(resourcePath);
        return doOperation(request, queryArchivesLibrariesResponseParser, true);
    }

    /**
     * 批量获取人员档案库
     *
     * @param queryProfileLibrariesRequest
     * @return
     */
    public QueryProfileLibrariesResponse
    queryProfileLibraries(QueryProfileLibrariesRequest queryProfileLibrariesRequest) {
        String xml = getXmlFromObject(queryProfileLibrariesRequest, QueryProfileLibrariesRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request =
                new VCMRequestMessageBuilder(this.client).setInputStream(in).setInputSize(xml.getBytes().length).setEndpoint(this.endpoint).setMethod(HttpMethod.POST).build();
        request.setResourcePath(BATCH_QUERY_ARCHIVES_LIBRARIES);
        return doOperation(request, queryArchivesLibrariesResponseParser, true);
    }

    /**
     * 获取所有档案库信息
     *
     * @return
     */
    public QueryProfileLibrariesResponse queryAll() {
        RequestMessage request = new VCMRequestMessageBuilder(this.client).setEndpoint(endpoint).setMethod(HttpMethod.GET).build();
        request.setResourcePath(PROFILE_LIBRARIES);
        return doOperation(request, queryArchivesLibrariesResponseParser, true);
    }

    /**
     * 增加人员档案库，如重点人员档案库、高频档案库等，档案库的个数限制128
     *
     * @param addProfileLibrariesRequest
     * @return
     */
    public AddProfileLibrariesResponse addProfileLibraries(AddProfileLibrariesRequest addProfileLibrariesRequest) {
        String xml = getXmlFromObject(addProfileLibrariesRequest, AddProfileLibrariesRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request =
                new VCMRequestMessageBuilder(this.client).setEndpoint(endpoint).setInputStream(in).setInputSize(xml.getBytes().length).setMethod(HttpMethod.POST).build();
        request.setResourcePath(PROFILE_LIBRARIES);
        ResponseParsers.GlobalResponseParser<AddProfileLibrariesResponse> globalResponseParser = new ResponseParsers.GlobalResponseParser<>(AddProfileLibrariesResponse.class);
        return doOperation(request, globalResponseParser, true);
    }

    /**
     * 更新人员档案库，支持更新名字、描述、tag
     *
     * @param updateProfileLibrariesRequest
     * @return
     */
    public VCMResponse updateProfileLibraries(UpdateProfileLibrariesRequest updateProfileLibrariesRequest) {
        String xml = getXmlFromObject(updateProfileLibrariesRequest, UpdateProfileLibrariesRequest.class);
        InputStream in = new ByteArrayInputStream(xml.getBytes());
        RequestMessage request =
                new VCMRequestMessageBuilder(this.client).setEndpoint(endpoint).setInputStream(in).setInputSize(xml.getBytes().length).setMethod(HttpMethod.PUT).build();
        String resourcePath = PROFILE_LIBRARIES + "/" + updateProfileLibrariesRequest.getId();
        request.setResourcePath(resourcePath);
        return doOperation(request, emptyResponseParser, true);
    }

    /**
     * <p>
     * <删除人员档案库/p>
     * <p>
     * 1、删除普通档案库，不删除档案，只删除档案和档案库的映射关系。
     * </p>
     * <p>
     * 2、删除有名档/无名档，直接删除档案，包括档案中的数据。
     * </p>
     *
     * @param id
     * @return
     */
    public VCMResponse deleteProfileLibraries(String id) {
        RequestMessage request = new VCMRequestMessageBuilder(this.client).setEndpoint(endpoint).setMethod(HttpMethod.DELETE).build();
        request.setResourcePath(PROFILE_LIBRARIES + "/" + id);
        return doOperation(request, emptyResponseParser, true);
    }

    /**
     * 删除所有人员档案库 1、删除所有档案库，不删除档案，
     *
     * @return
     */
    public VCMResponse deleteAllProfileLibraries() {
        RequestMessage request = new VCMRequestMessageBuilder(this.client).setEndpoint(endpoint).setMethod(HttpMethod.DELETE).build();
        request.setResourcePath(PROFILE_LIBRARIES);
        return doOperation(request, emptyResponseParser, true);
    }

}
